<!-- 我的 - 隐私设置 -->
<template>
  <view>
    <z-navbar title="隐私设置" :border-bottom="true"></z-navbar>
    <view class="px-4">
      <u-form :model="state.formData" ref="form" :label-style="labelStyle" label-width="260"
        :error-type="state.errorType">
        <u-form-item label="浏览记录">
          <view class="flex items-center">
            <u-radio-group v-model="state.formData.history" active-color="#F4AF22" @change="radioGroupChange">
              <u-radio v-for="(item, index) in priVateRadioList" :key="index" :name="item.value"
                :disabled="item.disabled">
                {{ item.name }}
              </u-radio>
            </u-radio-group>
          </view>
        </u-form-item>
        <u-form-item label="我的学社">
          <view class="flex items-center">
            <u-radio-group v-model="state.formData.society" active-color="#F4AF22" @change="radioGroupChange">
              <u-radio v-for="(item, index) in priVateRadioList" :key="index" :name="item.value"
                :disabled="item.disabled">
                {{ item.name }}
              </u-radio>
            </u-radio-group>
          </view>
        </u-form-item>
      </u-form>
    </view>
    <view class="btn-submit mt-2" @click="onLogout">退出登录</view>
  </view>
</template>

<script setup>
import { showToast,showModal } from "@/common/util/uni";
import { useUserStore } from '@/store/user'
import { priVateRadioList } from '@/common/constant/user.js'
import { postUserSetPrivate } from '@/common/http/module/center.js'


const userStore = useUserStore()
const state = reactive({
  errorType: ['toast'],
  formData: {
    history: userStore.userInfo?.history_open ?? 1,
    society: userStore.userInfo?.club_open ?? 1,
  },
})
const form = ref()

// 表单label样式
const labelStyle = {
  fontFamily: 'PingFang SC',
  color: '#333333',
  fontWeight: 'bold',
  fontSize: '28rpx'
}

function onLogout() {
  showModal({
    title: '提示',
    content: '确定要退出登录吗?',
    success: ({ confirm }) => {
      if (!confirm) return
      userStore.logout()
      uni.switchTab({
        url: '/pages/user/index'
      });
    },
  })
}
// 选中任一radio时，由radio-group触发
function radioGroupChange () {
  postUserSetPrivate({
    history_open: state.formData.history,
    club_open: state.formData.society
  }).then(() => {
    // 刷新个人信息
    userStore.loadUserInfo().then(() => {
      showToast('设置成功')
    })
  })
}
</script>

<style lang="scss" scoped></style>
